package cn.wgx.module.mind.websocket.util;

import cn.wgx.common.util.SpringContextHolder;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

/**
 * 用户禁言工具类
 */
public class UserForbidden {

    private static RedisTemplate redisTemplate;

    private static final String CHAT_ROOM_ALL_FORBIDDEN_KEY = "CHAT_ROOM_USER_FORBIDDEN_KEY_";

    static {
        redisTemplate = SpringContextHolder.getBean("redisTemplate");
    }


    //判断用户是否禁言
    public static boolean isForbidden(String roomId, String userId){
        return redisTemplate.opsForSet().isMember(CHAT_ROOM_ALL_FORBIDDEN_KEY + roomId, userId);
    }

    //禁言某用户
    public static void forbidden(String roomId, String userId){
        redisTemplate.opsForSet().add(CHAT_ROOM_ALL_FORBIDDEN_KEY+ roomId, userId);
    }

    //解禁某用户
    public static void unForbidden(String roomId, String userId){
        redisTemplate.opsForSet().remove(CHAT_ROOM_ALL_FORBIDDEN_KEY+ roomId, userId);
    }

    //所有被禁言用户ID
    public static Set getRoomAllUser(String roomId){
        return redisTemplate.opsForSet().members(CHAT_ROOM_ALL_FORBIDDEN_KEY+ roomId);
    }

}
